/*************************************************************************
    > File Name: client.cpp
    > Author: Lancer
    > Mail: 18826075768@163.com
************************************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>

using namespace std;


struct msg_st{
    long int msg_type;
    char text[BUFSIZ]; 
};
int main(){
    int running = 1;
    int msgid = -1;
    struct msg_st data;
    long int msgtype = 0;
    msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
    if(msgid == -1){
        fprintf(stderr, "msgget failed with error: %d\n", errno);
        exit(EXIT_FAILURE);
    }
    while(running){
        if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1){
            fprintf(stderr, "msgrcv failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }
        printf("you wrote: %s", data.text);

        if(strncmp(data.text, "end", 3) == 0){
            running = 0;
        }
    }

    if(msgctl(msgid, IPC_RMID, 0) == -1){
        fprintf(stderr, "msgctl(IPC_RMID) failed with error: %d\n", errno);
        exit(EXIT_FAILURE);
    }
    return 0;
}